from flask import Blueprint,request,jsonify
from flask_restful import reqparse
from db import db
user_db=Blueprint('user_db',__name__)

# 登录
@user_db.route('/login',methods=['POST'])
def login():
   if request.method=='POST':
       res=reqparse.RequestParser()
       res.add_argument('phone',required=True)
       res.add_argument('pwd',required=True)
       res.add_argument('type',required=True)
       args=res.parse_args()
       sql = "select phone from user where phone=%s" % (args['phone'])
       res=db.find(sql)
       if not res:
            print(111)
            pho="insert into user(phone,pwd,type) values('%s','%s')"%(args['phone'],args['pwd'],args['type'])
            db.update(pho)
            db.commit()
            return jsonify({'code':200,'msg':'注册登录成功'})
       else:
           typee="select type from user where type=%s"%(args['type'])
           res=db.find(typee)  # 这样返回的是一个列表
           print(res['type']) # 这样获取的是一个字段里的数据 1
           if int(res['type'])==1:
               return jsonify({'code':201,'msg':'管理员登录成功','data':res})
           else:
                return jsonify({'code':202,'msg':'用户登录成功','data':res})
   elif request.method=='GET':
       sql = "select type from user"
       res = db.find(sql)
       return jsonify({'code':200,'data':res})


# 用户新闻发布会
@user_db.route('/user',methods=['POST','GET'])
def user():
    if request.method=='GET':
        sql="select * from content"
        res=db.find_all(sql)
        return jsonify({'code':200,'msg':'获取新闻数据成功','data':res})

    if request.method=='POST':
        res=reqparse.RequestParser()
        res.add_argument('title',required=True)
        res.add_argument('content1',required=True)
        res.add_argument('type',required=True)
        args=res.parse_args()
        type1="select type from user where type=%s"%args['type']
        res=db.find(type1)
        # 将获取的数据
        if int(res['type'])==0:
            sql = "insert into content(title,content1) values('%s','%s')" % (args['title'], args['content1'])
            print(11111)
            db.update(sql)
            db.commit()
            return jsonify({'code': 200, 'msg': '发布新闻成功审核中.....'})
        else:
            return jsonify({'code': 201, 'msg': '管理员审核中...'})


# 进行分页
@user_db.route('/limitt')
def limitt():
    sql="select type from user"
    res=db.find(sql)
    if int(res['type'])==1:
        sql="select * from content limit 0,3"
        ress=db.find(sql)
        return jsonify({'code':200,'msg':'对数据进行分页','data':ress})
    return 111


# 审核新闻并写入审核记录表
@user_db.route('/audit',methods=['POST','GET'])
def audit():
    if request.method == 'GET':
        sql = "select type from user"
        res = db.find(sql)
        if int(res['type']) == 1:
            sql="select * from audit"
            res=db.find_all(sql)
            return jsonify({'code':200,'msg':'获取所有数据成功','data':res})
        else:
            return jsonify({'code':201,'msg':'用户未开发'})
    if request.method=='POST':
        req = reqparse.RequestParser()
        req.add_argument('checks', required=True)
        args = req.parse_args()
        sql = "select type from user"
        res = db.find(sql)
        if int(res['type']) == 1:
            sql = "insert into audit(checks) values('%s')" % (args['checks'])
            db.update(sql)
            db.commit()
            return jsonify({'code':200,'msg':'管理员添加审核表成功'})
        else:
            return jsonify({'code':201,'msg':'用户尚未开发此功能'})

# 管理员添加商品
@user_db.route('/adddd',methods=['POST','GET'])
def adddd():
    if request.method=='GET':
        sql="select * from addd"
        res=db.find_all(sql)
        reslist=[]
        print(res)
        reslist.append(res)
        return jsonify({'code':200,'msg':'获取添加商品数据成功','data':reslist})
    elif request.method=='POST':
        req = reqparse.RequestParser()
        req.add_argument('shangpin',required=True)
        req.add_argument('price',required=True)
        args = req.parse_args()
        sql="select type from user"
        res=db.find(sql)
        if int(res['type'])==1:
            print(111111)
            sql="insert into addd(shangpin,price) values('%s','%s')"%(args['shangpin'],args['price'])
            db.update(sql)
            db.commit()
            # print(res)
            return jsonify({'code':200,'msg':'添加成功'})
        else:
            return jsonify({'code':200,'msg':'用户等待中'})



# 无限极分类
@user_db.route('/test')
def test():
    list=[{
        'id':1,
        'name':'111',
        'pid':0
    },{
        'id':2,
        'name':'112',
        'pid':1
    },{
        'id':3,
        'name':'113',
        'pid':2
    }]
    dict={}
    for i in list:
        dict[i['id']]=i
    reslist=[]
    for j in list:
        pid=j['pid']
        if pid==0:
            reslist.append(j)
        else:
            if 'son' not in dict[pid]:
                dict[pid]['son']=[]
            dict[pid]['son'].append(j)
    return jsonify({"li":reslist})


